package com.rongshu.view.userInfo.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Common;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.userInfo.adapter.PayDetailAdapter;
import com.rongshu.view.userInfo.entity.PayDetail;
import com.rongshu.view.userInfo.iview.PayDetailView;
import com.rongshu.view.userInfo.presenter.PayDetailPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class PayDetailActivity extends BaseActivity<PayDetailPresenter> implements PayDetailView {
    @BindView(R.id.txt_top_title)
    TextView txt_top_title;//标题
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    //manager
    private LinearLayoutManager manager = null;
    //adapter
    private PayDetailAdapter adapter = null;
    //map
    private Map<String, Object> map = null;
    private int pageIndex = 1;
    private int pageSize = 10;
    //refresh,load
    private boolean isrefresh = true;

    @Override
    protected int initLayout() {
        return R.layout.activity_pay_detail_view;
    }

    @Override
    protected void initView() {
        //set presenter
        setPresenter(new PayDetailPresenter(this), this);
        //设置title
        txt_top_title.setText(R.string.pay_detail);
        //manager
        manager = new LinearLayoutManager(this);
        //adapter
        adapter = new PayDetailAdapter(this);
        //recyclerview
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
        //触发自动刷新
        refreshLayout.autoRefresh();
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(final RefreshLayout refreshlayout) {
                isrefresh = true;
                pageIndex = 1;
                refreshLayout.resetNoMoreData();
                getTopUpRecord();
                refreshlayout.finishRefresh(2000, false);
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                isrefresh = false;
                pageIndex++;
                getTopUpRecord();
                refreshlayout.finishLoadmore(2000, false);
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    /**
     * 获取充值/消费记录
     */
    private void getTopUpRecord() {
        int user_id = UserInfoUtils.getUserId();
        map = new HashMap<>();
        map.put("user_id", user_id);
        map.put("type", "A");
        map.put("page", pageIndex);
        map.put("pagesize", pageSize);
        presenter.getTopUpRecord(user_id, "A", pageIndex, pageSize, Common.encryptionSign(map));
        map.clear();
    }


    @OnClick({R.id.layout_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_back:
                finish();
                break;
        }
    }

    /**
     * 获取充值/消费记录
     *
     * @param payDetail
     */
    @Override
    public void onTopUpRecord(PayDetail payDetail) {
        if (payDetail.getErrCode() == 0) {
            List<PayDetail.ResponseBean> responseBeans = payDetail.getResponse();
            if (adapter != null) {
                if (isrefresh) {
                    refreshLayout.finishRefresh();
                    if (responseBeans == null || responseBeans.isEmpty()) {
                        refreshLayout.setVisibility(View.GONE);
//                        layout_empty.setVisibility(View.VISIBLE);
                    } else {
                        refreshLayout.setVisibility(View.VISIBLE);
//                        layout_empty.setVisibility(View.GONE);
                        adapter.setPayDetails(responseBeans);
                        adapter.notifyDataSetChanged();
                    }
                } else {
                    if (responseBeans == null || responseBeans.isEmpty()) {
                        refreshLayout.finishLoadmoreWithNoMoreData();
                    } else {
                        refreshLayout.finishLoadmore();
                        adapter.addPayDetails(responseBeans);
                        adapter.notifyDataSetChanged();
                    }
                }
            }
        } else {
            toast(payDetail.getErrMsg());
        }
    }
}
